﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System;
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;

namespace Peter.Logic.AvalonEdit
{
   /// <summary>
   /// A background renderer for highlighting the current line.
   /// </summary>
   public class HighlightCurrentLineBackgroundRenderer : IBackgroundRenderer
   {
      private readonly TextEditor m_Editor;

      /// <summary>
      /// Initializes a new Highlight Current Line Background Renderer
      /// </summary>
      /// <param name="editor">The editor to attach to.</param>
      public HighlightCurrentLineBackgroundRenderer (TextEditor editor)
      {
         this.m_Editor = editor;
         this.HighlightBrush = Brushes.LightGoldenrodYellow;
         this.BorderPen = new Pen (Brushes.LightGray, 1);
      }

      /// <summary>
      /// Gets the layer to draw this on.
      /// </summary>
      public KnownLayer Layer
      {
         get { return KnownLayer.Background; }
      }

      /// <summary>
      /// Gets or Sets the brush to highlight with.
      /// </summary>
      public Brush HighlightBrush { get; set; }

      /// <summary>
      /// Gets or Sets the border pen.
      /// </summary>
      public Pen BorderPen { get; set; }

      /// <summary>
      /// Adds this background renderer to the stack of renderers.
      /// </summary>
      public void Add ()
      {
         if (!this.m_Editor.TextArea.TextView.BackgroundRenderers.Contains (this))
            this.m_Editor.TextArea.TextView.BackgroundRenderers.Add (this);
      }

      /// <summary>
      /// Removes this background renderer from the stack of renderers.
      /// </summary>
      public void Remove ()
      {
         if (this.m_Editor.TextArea.TextView.BackgroundRenderers.Contains (this))
            this.m_Editor.TextArea.TextView.BackgroundRenderers.Remove (this);
      }

      /// <summary>
      /// Draws the line highlight
      /// </summary>
      /// <param name="textView">The text view with the line highlighted.</param>
      /// <param name="drawingContext"></param>
      public void Draw (TextView textView, DrawingContext drawingContext)
      {
         textView.EnsureVisualLines ();
         var line = textView.Document.GetLineByOffset (this.m_Editor.CaretOffset);
         var segment = new TextSegment { StartOffset = line.Offset, EndOffset = line.EndOffset };
         var drawRect = new Rect (0, Double.PositiveInfinity, textView.ActualWidth, 0);
         foreach (var r in BackgroundGeometryBuilder.GetRectsForSegment (textView, segment))
         {
            if (r.Y < drawRect.Y)
               drawRect.Y = r.Y;
            drawRect.Height += r.Height;
         }
         drawingContext.DrawRectangle (this.HighlightBrush, this.BorderPen, drawRect);
      }
   }
}
